import 'dart:convert';
import 'package:demo/util/asset.dart';
import 'package:flutter/material.dart';

class TotalDetailCom extends StatefulWidget {
  @override
  _TotalDetailComState createState() => _TotalDetailComState();
}

class _TotalDetailComState extends State<TotalDetailCom> {
  var obj;
  int itemLength = 0;
  String type;
  bool isTab = false;

  // 初始化数据
  void initData() {
    isTab = SpUtil.preferences.getBool('isTab');
    type = SpUtil.preferences.getString('totalType');
    if (isTab) {
      obj = jsonDecode(SpUtil.preferences.getString('myData'));
    } else {
      if (type == '今天') {
        obj = jsonDecode(SpUtil.preferences.getString('thisDay'));
      }
      if (type == '本月') {
        obj = jsonDecode(SpUtil.preferences.getString('thisMonth'));
      }
      if (type == '本年') {
        obj = jsonDecode(SpUtil.preferences.getString('thisYear'));
      }
    }
  }

  // 列表数据
  List<Widget> _getList() {
    String type = SpUtil.preferences.getString('totalType');
    List<Widget> temList = [];

    // isTab判断为点击底部tab切换时，展示所有账单信息
    if (isTab) {
      var dayDetail;
      for (var yearItem in obj) {
        for (var item in yearItem['monthList']) {
          for (var j = 0; j < item['dayList'].length; j++) {
            dayDetail = item['dayList'][j]['dayDetail'];
            for (var i = 0; i < dayDetail.length; i++) {
              temList.add(_moneyItem(dayDetail[i], true));
            }
          }
        }
      }
    } else {
      if (type == '今天') {
        var dayDetail = obj['dayDetail'];
        for (var i = 0; i < dayDetail.length; i++) {
          temList.add(_moneyItem(dayDetail[i], false));
        }
        // temList = obj['dayDetail'].map((item) {
        //   return ListTile(
        //     title: Text(item['time']),
        //   );
        // });
      }
      if (type == '本月') {
        var dayDetail;
        for (var item in obj['dayList']) {
          dayDetail = item['dayDetail'];
          for (var i = 0; i < dayDetail.length; i++) {
            temList.add(_moneyItem(dayDetail[i], true));
          }
        }
      }
      if (type == '本年') {
        var dayDetail;
        for (var item in obj['monthList']) {
          for (var j = 0; j < item['dayList'].length; j++) {
            dayDetail = item['dayList'][j]['dayDetail'];
            for (var i = 0; i < dayDetail.length; i++) {
              temList.add(_moneyItem(dayDetail[i], true));
            }
          }
        }
      }
    }
    return temList;
  }

  // 单挑数据DOM
  Container _moneyItem(item, time) {
    var t = DateTime.fromMillisecondsSinceEpoch(item['time']);
    String year = t.year.toString();
    String month = t.month.toString();
    String day = t.day.toString();
    String allTime = '$year - $month - $day';
    return Container(
      height: 70.0,
      margin: EdgeInsets.all(10.0),
      padding: EdgeInsets.only(left: 10.0, right: 10.0),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(color: Colors.black45, width: 1.0),
        ),
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                flex: 1,
                child: Text(
                  item['select'],
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 18.0,
                  ),
                ),
              ),
              Expanded(
                flex: 2,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    time
                        ? Text(allTime,
                            style: TextStyle(
                              color: Colors.black45,
                              fontSize: 14.0,
                            ))
                        : Text(''),
                  ],
                ),
              ),
              Text(
                item['type'],
              ),
            ],
          ),
          SizedBox(height: 10),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                item['remark'],
              ),
              Text(
                item['type'] == '收入'
                    ? item['sumIncome'].toString()
                    : item['sumSpend'].toString(),
                style: TextStyle(
                  fontSize: 16.0,
                  fontWeight: FontWeight.bold,
                  color: item['type'] == '收入'
                      ? Colors.green[400]
                      : Colors.red[400],
                ),
              )
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    initData();
    return Container(
      width: MediaQuery.of(context).size.width,
      child: _getList().length == 0
          ? Container(
              width: MediaQuery.of(context).size.width,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    '没有账单数据！',
                    style: TextStyle(color: Colors.black54, fontSize: 30.0),
                  ),
                ],
              ),
            )
          : SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.only(bottom: 40.0),
                child: Column(
                  children: _getList(),
                ),
              ),
            ),
    );
  }
}
